package net.oschina.app.viewpagerfragment.tweetDetailFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.TweetNewsAdapter;
import net.oschina.app.bean.Tweet;
import net.oschina.app.bean.TweetsList;
import net.oschina.app.interf.OnRecycleViewItemClickListener;
import net.oschina.app.ui.TweetDetailActivity;
import net.oschina.app.util.XmlUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by CXB on 2017/4/5.
 */

public abstract class BaseTweetFragment extends Fragment {
    private TweetsList mTweetsList = new TweetsList();
    private TweetNewsAdapter mNewsAdapter;
    private XRecyclerView mXRecyclerView;
    private List<Tweet> mTweets;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取数据
        initData();
        View view =inflater.inflate(R.layout.tweet_new_fragment,container,false);
        mXRecyclerView = (XRecyclerView)view.findViewById(R.id.item_xr_sweet_icon);
        LinearLayoutManager manager =new LinearLayoutManager(getActivity());
        mXRecyclerView.setLayoutManager(manager);

        return view;
    }
    //获取数据
    private void initData() {
        OkHttpClient client =new OkHttpClient();
        String url =getUrl();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();
                mTweetsList = XmlUtils.toBean(TweetsList.class, inputStream);
                mTweets = mTweetsList.getTweetslist();
                System.out.println("dsa");
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mNewsAdapter = new TweetNewsAdapter(getActivity(),mTweetsList);
                        mXRecyclerView.setAdapter(mNewsAdapter);
                        //事件处理
                        initEvent();
                    }
                });
            }
        });
    }
    //事件处理
    private void initEvent() {
        mNewsAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getActivity(), TweetDetailActivity.class);
                intent.putParcelableArrayListExtra("mTweets", (ArrayList<? extends Parcelable>) mTweets);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    public abstract String getUrl();
}
